package com.mjfeed.controller.shop.member;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.BrowseLog;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.Product;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.OrderService;
import com.mjfeed.service.ProductCategoryService;
import com.mjfeed.service.ProductService;
import com.mjfeed.util.Md5Util;
import com.mjfeed.util.WebUtils;

/**
 * @description 会员
 * @author zhaominglei
 * @date 2014年10月7日
 */
@Controller("shopMemberController")
@RequestMapping("/member")
public class MemberController extends BaseController {
	/** 最新订单数 */
	@SuppressWarnings("unused")
	private static final int NEW_ORDER_COUNT = 10;
	
	@Resource(name = "memberService")
	private MemberService memberService;
	@Resource(name="productService")
	private ProductService productService;
	@Resource(name = "orderService")
	private OrderService orderService;
	@Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
	/**
	 * 首页
	 */
	@RequestMapping(value = "/index", method=RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
//		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
//		queryConditions.add(new QueryCondition("member", QueryCondition.EQ, member));
//		String orderBy = "order by id desc";
//		request.setAttribute("orders", orderService.get(Order.class, queryConditions, orderBy, 1, NEW_ORDER_COUNT));
		List<Product> products = new ArrayList<Product>();
		Set<BrowseLog> browseLogs = member.getBrowseLogs();
		if (browseLogs != null && browseLogs.size() > 0) {
			for (BrowseLog browseLog : browseLogs) {
				if (browseLog.getProduct() == null || browseLog.getProduct().getId().intValue() < 1) {
					continue;
				}
				Product product = productService.getById(Product.class, browseLog.getProduct().getId());
				if (product != null && !products.contains(product)) {
					products.add(product);
				}
			}
		}
		Collections.shuffle(products);
		request.setAttribute("likeProducts", products);
		request.setAttribute("productCategoryTree", productCategoryService.findTree());
		request.setAttribute("member", member);
		return "shop/member/index";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getCurrent());
		return "shop/member/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(Member member, HttpServletRequest request, HttpServletResponse response, Model model){
		Member pMember = memberService.getById(Member.class, member.getId());
		pMember.setEmail(member.getEmail());
		pMember.setName(member.getName());
		pMember.setMobile(member.getMobile());
		memberService.update(pMember);
		return "shop/member/edit";
	}
	
	@RequestMapping(value = "/editpassword", method=RequestMethod.GET)
	public String editpassword(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getCurrent());
		return "shop/member/editpassword";
	}
	
	@RequestMapping(value = "/updatepassword", method=RequestMethod.POST)
	public String updatepassword(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		String pPassword = request.getParameter("pPassword");
		String nPassword = request.getParameter("nPassword");
		String cfnPassword = request.getParameter("cfnPassword");
		if (StringUtils.isEmpty(pPassword) || StringUtils.isEmpty(nPassword) || StringUtils.isEmpty(cfnPassword)) {
			return ERROR_VIEW;
		}
		if (!cfnPassword.equals(nPassword)) {
			return ERROR_VIEW;
		}
		Member pMember = memberService.getById(Member.class, id);
		if (!Md5Util.encrypt(pPassword).equals(pMember.getPassword())) {
			return ERROR_VIEW;
		}
		pMember.setPassword(Md5Util.encrypt(nPassword));
		memberService.update(pMember);
		request.getSession().removeAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
		WebUtils.removeCookie(request, response, Member.USERNAME_COOKIE_NAME);
		return "redirect:/login.shtml";
	}
}
